package com.google.zxing.util;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build.VERSION;
import android.util.Log;
import android.view.Display;
import android.view.DisplayCutout;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;

import java.lang.reflect.Method;

/*
 * created by wdlmm1214
 * on 2019/12/23
 */public class QMUINotchHelper {
    private static final String TAG = "QMUINotchHelper";
    private static final int NOTCH_IN_SCREEN_VOIO = 32;
    private static final String MIUI_NOTCH = "ro.miui.notch";
    private static Boolean sHasNotch = null;
    private static Rect sRotation0SafeInset = null;
    private static Rect sRotation90SafeInset = null;
    private static Rect sRotation180SafeInset = null;
    private static Rect sRotation270SafeInset = null;
    private static int[] sNotchSizeInHawei = null;
    private static Boolean sHuaweiIsNotchSetToShow = null;
    private static Boolean sXiaomiIsNotchSetToShow = null;

    public QMUINotchHelper() {
    }

    public static boolean hasNotchInVivo(Context context) {
        boolean ret = false;

        try {
            ClassLoader cl = context.getClassLoader();
            Class ftFeature = cl.loadClass("android.util.FtFeature");
            Method[] methods = ftFeature.getDeclaredMethods();
            if (methods != null) {
                for(int i = 0; i < methods.length; ++i) {
                    Method method = methods[i];
                    if (method.getName().equalsIgnoreCase("isFeatureSupport")) {
                        ret = (Boolean)method.invoke(ftFeature, 32);
                        break;
                    }
                }
            }
        } catch (ClassNotFoundException var7) {
            Log.i("QMUINotchHelper", "hasNotchInVivo ClassNotFoundException");
        } catch (Exception var8) {
            Log.e("QMUINotchHelper", "hasNotchInVivo Exception");
        }

        return ret;
    }

    public static boolean hasNotchInHuawei(Context context) {
        boolean hasNotch = false;

        try {
            ClassLoader cl = context.getClassLoader();
            Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
            Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
            hasNotch = (Boolean)get.invoke(HwNotchSizeUtil);
        } catch (ClassNotFoundException var5) {
            Log.i("QMUINotchHelper", "hasNotchInHuawei ClassNotFoundException");
        } catch (NoSuchMethodException var6) {
            Log.e("QMUINotchHelper", "hasNotchInHuawei NoSuchMethodException");
        } catch (Exception var7) {
            Log.e("QMUINotchHelper", "hasNotchInHuawei Exception");
        }

        return hasNotch;
    }

    public static boolean hasNotchInOppo(Context context) {
        return context.getPackageManager().hasSystemFeature("com.oppo.feature.screen.heteromorphism");
    }

    public static boolean hasNotchInXiaomi(Context context) {
        try {
            Class spClass = Class.forName("android.os.SystemProperties");
            Method getMethod = spClass.getDeclaredMethod("getInt", String.class, Integer.class);
            int hasNotch = (Integer)getMethod.invoke((Object)null, "ro.miui.notch", 0);
            return hasNotch == 1;
        } catch (Exception var4) {
            return false;
        }
    }

    public static boolean hasNotch(View view) {
        if (sHasNotch == null) {
            if (isNotchOfficialSupport()) {
                WindowInsets windowInsets = view.getRootWindowInsets();
                if (windowInsets == null) {
                    return false;
                }

                DisplayCutout displayCutout = windowInsets.getDisplayCutout();
                sHasNotch = displayCutout != null;
            } else {
                sHasNotch = has3rdNotch(view.getContext());
            }
        }

        return sHasNotch;
    }

    public static boolean hasNotch(Activity activity) {
        if (sHasNotch == null) {
            if (isNotchOfficialSupport()) {
                Window window = activity.getWindow();
                if (window == null) {
                    return false;
                }

                View decorView = window.getDecorView();
                if (decorView == null) {
                    return false;
                }

                DisplayCutout displayCutout = decorView.getRootWindowInsets().getDisplayCutout();
                sHasNotch = displayCutout != null;
            } else {
                sHasNotch = has3rdNotch(activity);
            }
        }

        return sHasNotch;
    }

    private static boolean has3rdNotch(Context context) {
        if (QMUIDeviceHelper.isHuawei()) {
            return hasNotchInHuawei(context);
        } else if (QMUIDeviceHelper.isVivo()) {
            return hasNotchInVivo(context);
        } else if (QMUIDeviceHelper.isOppo()) {
            return hasNotchInOppo(context);
        } else {
            return QMUIDeviceHelper.isXiaomi() ? hasNotchInXiaomi(context) : false;
        }
    }

    public static int getSafeInsetTop(Activity activity) {
        return !hasNotch(activity) ? 0 : getSafeInsetRect(activity).top;
    }

    public static int getSafeInsetBottom(Activity activity) {
        return !hasNotch(activity) ? 0 : getSafeInsetRect(activity).bottom;
    }

    public static int getSafeInsetLeft(Activity activity) {
        return !hasNotch(activity) ? 0 : getSafeInsetRect(activity).left;
    }

    public static int getSafeInsetRight(Activity activity) {
        return !hasNotch(activity) ? 0 : getSafeInsetRect(activity).right;
    }

    public static int getSafeInsetTop(View view) {
        return !hasNotch(view) ? 0 : getSafeInsetRect(view).top;
    }

    public static int getSafeInsetBottom(View view) {
        return !hasNotch(view) ? 0 : getSafeInsetRect(view).bottom;
    }

    public static int getSafeInsetLeft(View view) {
        return !hasNotch(view) ? 0 : getSafeInsetRect(view).left;
    }

    public static int getSafeInsetRight(View view) {
        return !hasNotch(view) ? 0 : getSafeInsetRect(view).right;
    }

    private static void clearAllRectInfo() {
        sRotation0SafeInset = null;
        sRotation90SafeInset = null;
        sRotation180SafeInset = null;
        sRotation270SafeInset = null;
    }

    private static void clearPortraitRectInfo() {
        sRotation0SafeInset = null;
        sRotation180SafeInset = null;
    }

    private static void clearLandscapeRectInfo() {
        sRotation90SafeInset = null;
        sRotation270SafeInset = null;
    }

    private static Rect getSafeInsetRect(Activity activity) {
        if (isNotchOfficialSupport()) {
            Rect rect = new Rect();
            DisplayCutout displayCutout = activity.getWindow().getDecorView().getRootWindowInsets().getDisplayCutout();
            if (displayCutout != null) {
                rect.set(displayCutout.getSafeInsetLeft(), displayCutout.getSafeInsetTop(), displayCutout.getSafeInsetRight(), displayCutout.getSafeInsetBottom());
            }

            return rect;
        } else {
            return get3rdSafeInsetRect(activity);
        }
    }

    private static Rect getSafeInsetRect(View view) {
        if (isNotchOfficialSupport()) {
            Rect rect = new Rect();
            DisplayCutout displayCutout = view.getRootWindowInsets().getDisplayCutout();
            if (displayCutout != null) {
                rect.set(displayCutout.getSafeInsetLeft(), displayCutout.getSafeInsetTop(), displayCutout.getSafeInsetRight(), displayCutout.getSafeInsetBottom());
            }

            return rect;
        } else {
            return get3rdSafeInsetRect(view.getContext());
        }
    }

    private static Rect get3rdSafeInsetRect(Context context) {
        boolean isXiaomiNotchSetToShow;
        if (QMUIDeviceHelper.isHuawei()) {
            isXiaomiNotchSetToShow = QMUIDisplayHelper.huaweiIsNotchSetToShowInSetting(context);
            if (sHuaweiIsNotchSetToShow != null && sHuaweiIsNotchSetToShow != isXiaomiNotchSetToShow) {
                clearLandscapeRectInfo();
            }

            sHuaweiIsNotchSetToShow = isXiaomiNotchSetToShow;
        } else if (QMUIDeviceHelper.isXiaomi()) {
            isXiaomiNotchSetToShow = QMUIDisplayHelper.xiaomiIsNotchSetToShowInSetting(context);
            if (sXiaomiIsNotchSetToShow != null && sXiaomiIsNotchSetToShow != isXiaomiNotchSetToShow) {
                clearAllRectInfo();
            }

            sXiaomiIsNotchSetToShow = isXiaomiNotchSetToShow;
        }

        int screenRotation = getScreenRotation(context);
        if (screenRotation == 1) {
            if (sRotation90SafeInset == null) {
                sRotation90SafeInset = getRectInfoRotation90(context);
            }

            return sRotation90SafeInset;
        } else if (screenRotation == 2) {
            if (sRotation180SafeInset == null) {
                sRotation180SafeInset = getRectInfoRotation180(context);
            }

            return sRotation180SafeInset;
        } else if (screenRotation == 3) {
            if (sRotation270SafeInset == null) {
                sRotation270SafeInset = getRectInfoRotation270(context);
            }

            return sRotation270SafeInset;
        } else {
            if (sRotation0SafeInset == null) {
                sRotation0SafeInset = getRectInfoRotation0(context);
            }

            return sRotation0SafeInset;
        }
    }

    private static Rect getRectInfoRotation0(Context context) {
        Rect rect = new Rect();
        if (QMUIDeviceHelper.isVivo()) {
            rect.top = getNotchHeightInVivo(context);
            rect.bottom = 0;
        } else if (QMUIDeviceHelper.isOppo()) {
            rect.top = StatusBarHelper.getStatusbarHeight(context);
            rect.bottom = 0;
        } else if (QMUIDeviceHelper.isHuawei()) {
            int[] notchSize = getNotchSizeInHuawei(context);
            rect.top = notchSize[1];
            rect.bottom = 0;
        } else if (QMUIDeviceHelper.isXiaomi()) {
            rect.top = getNotchHeightInXiaomi(context);
            rect.bottom = 0;
        }

        return rect;
    }

    private static Rect getRectInfoRotation90(Context context) {
        Rect rect = new Rect();
        if (QMUIDeviceHelper.isVivo()) {
            rect.left = getNotchHeightInVivo(context);
            rect.right = 0;
        } else if (QMUIDeviceHelper.isOppo()) {
            rect.left = StatusBarHelper.getStatusbarHeight(context);
            rect.right = 0;
        } else if (QMUIDeviceHelper.isHuawei()) {
            if (sHuaweiIsNotchSetToShow) {
                rect.left = getNotchSizeInHuawei(context)[1];
            } else {
                rect.left = 0;
            }

            rect.right = 0;
        } else if (QMUIDeviceHelper.isXiaomi()) {
            if (sXiaomiIsNotchSetToShow) {
                rect.left = getNotchHeightInXiaomi(context);
            } else {
                rect.left = 0;
            }

            rect.right = 0;
        }

        return rect;
    }

    private static Rect getRectInfoRotation180(Context context) {
        Rect rect = new Rect();
        if (QMUIDeviceHelper.isVivo()) {
            rect.top = 0;
            rect.bottom = getNotchHeightInVivo(context);
        } else if (QMUIDeviceHelper.isOppo()) {
            rect.top = 0;
            rect.bottom = StatusBarHelper.getStatusbarHeight(context);
        } else if (QMUIDeviceHelper.isHuawei()) {
            int[] notchSize = getNotchSizeInHuawei(context);
            rect.top = 0;
            rect.bottom = notchSize[1];
        } else if (QMUIDeviceHelper.isXiaomi()) {
            rect.top = 0;
            rect.bottom = getNotchHeightInXiaomi(context);
        }

        return rect;
    }

    private static Rect getRectInfoRotation270(Context context) {
        Rect rect = new Rect();
        if (QMUIDeviceHelper.isVivo()) {
            rect.right = getNotchHeightInVivo(context);
            rect.left = 0;
        } else if (QMUIDeviceHelper.isOppo()) {
            rect.right = StatusBarHelper.getStatusbarHeight(context);
            rect.left = 0;
        } else if (QMUIDeviceHelper.isHuawei()) {
            if (sHuaweiIsNotchSetToShow) {
                rect.right = getNotchSizeInHuawei(context)[1];
            } else {
                rect.right = 0;
            }

            rect.left = 0;
        } else if (QMUIDeviceHelper.isXiaomi()) {
            if (sXiaomiIsNotchSetToShow) {
                rect.right = getNotchHeightInXiaomi(context);
            } else {
                rect.right = 0;
            }

            rect.left = 0;
        }

        return rect;
    }

    public static int[] getNotchSizeInHuawei(Context context) {
        if (sNotchSizeInHawei == null) {
            sNotchSizeInHawei = new int[]{0, 0};

            try {
                ClassLoader cl = context.getClassLoader();
                Class HwNotchSizeUtil = cl.loadClass("com.huawei.android.util.HwNotchSizeUtil");
                Method get = HwNotchSizeUtil.getMethod("getNotchSize");
                sNotchSizeInHawei = (int[])((int[])get.invoke(HwNotchSizeUtil));
            } catch (ClassNotFoundException var4) {
                Log.e("QMUINotchHelper", "getNotchSizeInHuawei ClassNotFoundException");
            } catch (NoSuchMethodException var5) {
                Log.e("QMUINotchHelper", "getNotchSizeInHuawei NoSuchMethodException");
            } catch (Exception var6) {
                Log.e("QMUINotchHelper", "getNotchSizeInHuawei Exception");
            }
        }

        return sNotchSizeInHawei;
    }

    public static int getNotchWidthInXiaomi(Context context) {
        int resourceId = context.getResources().getIdentifier("notch_width", "dimen", "android");
        return resourceId > 0 ? context.getResources().getDimensionPixelSize(resourceId) : -1;
    }

    public static int getNotchHeightInXiaomi(Context context) {
        int resourceId = context.getResources().getIdentifier("notch_height", "dimen", "android");
        return resourceId > 0 ? context.getResources().getDimensionPixelSize(resourceId) : -1;
    }

    public static int getNotchWidthInVivo(Context context) {
        return QMUIDisplayHelper.dp2px(context, 100);
    }

    public static int getNotchHeightInVivo(Context context) {
        return QMUIDisplayHelper.dp2px(context, 27);
    }

    private static int getScreenRotation(Context context) {
        WindowManager w = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        if (w == null) {
            return 0;
        } else {
            Display display = w.getDefaultDisplay();
            return display == null ? 0 : display.getRotation();
        }
    }

    public static boolean isNotchOfficialSupport() {
        return VERSION.SDK_INT >= 28;
    }
}
